<?php
declare (strict_types = 1);

namespace app\adminapi\controller\system\organize;

use adminmatrix\server\app\server\JsonService;
use app\adminapi\logic\system\organize\CompanyLogic;
use app\adminapi\logic\system\organize\DepartmentLogic;
use think\exception\HttpException;
use think\Request;
use adminmatrix\server\app\adminMatrix;
use think\response\Json;
use adminmatrix\annotation\auth\IsPermission;
class DepartmentController extends AdminMatrix
{
    public function lists(){
        return $this->dataLists();
    }

    public function save(Request $request , DepartmentLogic $department) :Json
    {
        try {
            $department->save($request);
            return JsonService::success('新增部门成功');
        } catch (HttpException $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }

    public function update(Request $request , DepartmentLogic $department) : Json
    {
        try {
            $department->update($request);
            return JsonService::success('更新部门成功');
        } catch (HttpException $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }

    public function delete(int $id , Request $request , DepartmentLogic $department) : Json
    {

        try {
            $department->delete($id);
            return JsonService::success('删除成功');
        } catch (HttpException $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }


    #[IsPermission]
    public function getDepartmentList(Request $request ) : Json
    {
        try {
            $lists = (new DepartmentLogic($this->app)) -> get($request);
            return  JsonService::success('' , $lists);
        } catch (HttpException $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }
}
